package cn.com.lms.business.log.web;

import cn.com.lms.business.log.persistence.entity.UserActionLog;
import cn.com.lms.business.log.service.UserActionLogService;
import com.bnzj.core.persistence.query.CompareExpression;
import com.bnzj.core.persistence.query.CompareType;
import com.bnzj.core.persistence.query.IExpression;
import com.bnzj.core.persistence.query.QueryHelpBean;
import com.bnzj.core.rest.ResponseResult;
import com.bnzj.core.webmvc.util.WebMvcUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Schema;
import org.hibernate.internal.util.StringHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;

@RestController
@RequestMapping(value = "/system/user/log")
public class UserActionLogController {

    @Autowired
    UserActionLogService userActionLogService;

    @Operation(summary = "分页查询", description = "",
            parameters = {@Parameter(name = "actorName", description = "关键子查询，操作人名称进行模糊查询", allowEmptyValue = true,
                    allowReserved = true, schema = @Schema(implementation = String.class, description = "actorName")),
                    @Parameter(name = "type", description = "出题方式", allowEmptyValue = true,
                    allowReserved = true, schema = @Schema(implementation = Short.class, description = "type")),
                    @Parameter(name = "startTime", description = "开始时间", allowEmptyValue = true,
                            allowReserved = true, schema = @Schema(implementation = Date.class, description = "startTime")),
                    @Parameter(name = "endTime", description = "结束时间", allowEmptyValue = true,
                            allowReserved = true, schema = @Schema(implementation = Date.class, description = "endTime")),
                    @Parameter(name = "limit", description = "分页大小", allowEmptyValue = true, allowReserved = true, schema = @Schema(type = "int", description = "分页大小")),
                    @Parameter(name = "offset", description = "页码", allowEmptyValue = true, allowReserved = true, schema = @Schema(type = "int", description = "页码")),

            })
    @GetMapping(value = "/page")
    public ResponseResult<Page<UserActionLog>> getPaperPage(HttpServletRequest request) {
        Short type = StringHelper.isNotEmpty(request.getParameter("type")) ? Short.parseShort(request.getParameter("type")) : null;
        String actorName = request.getParameter("actorName");
        Pageable pageable = WebMvcUtils.initPage(request, Sort.by(Sort.Direction.DESC, "l.action_at"));
        QueryHelpBean queryHelpBean = userActionLogService.getQueryHelpBean();
        queryHelpBean.setPageable(pageable);
        //试卷名称，模糊搜索
        if (StringHelper.isNotEmpty(actorName)) {
            IExpression orCondition2 = CompareExpression.build("l.actor_name", CompareType.like, actorName);
            queryHelpBean.addWhere(orCondition2);
        }
        if (type != null) {
            IExpression andCondition3 = CompareExpression.build("l.action_type", CompareType.equal, type);
            queryHelpBean.addWhere(andCondition3);
        }

        Page<UserActionLog> all = userActionLogService.findAll(queryHelpBean);

        return ResponseResult.success(all);
    }


    @Operation(summary = "保存")
    @PostMapping("/save")
    public ResponseResult<?> addUserActionLog(@RequestBody UserActionLog userActionLog) {
        UserActionLog result=  userActionLogService.save(userActionLog);
        return ResponseResult.success(result);
    }


}
